﻿using System;

namespace Mozi.IoT.Generic
{
    //TODO 对引用 Revert的方法进行重构
    /// <summary>
    /// 公共静态扩展方法
    /// </summary>
    public static class Others
    {
        /// <summary>
        /// 数据翻转，不改变原始数据
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static byte[] Revert(this byte[] data)
        {
            byte[] d2 = new byte[data.Length];
            Array.Copy(data, d2, d2.Length);
            Array.Reverse(d2);
            return d2;
        }
        /// <summary>
        /// DateTime转Unix时间戳
        /// </summary>
        /// <param name="date"></param>
        /// <returns></returns>
        public static long ToTimestamp(this DateTime date)
        {
            var mills = (date.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0)).TotalMilliseconds;
            return (long)mills;
        }
        /// <summary>
        /// 转为字节数组
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        public static byte[] ToBytes(this ushort num)
        {
            return BitConverter.GetBytes(num);
        }
        /// <summary>
        /// 转为字节数组
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        public static byte[] ToBytes(this short num)
        {
            return BitConverter.GetBytes(num);
        }
        /// <summary>
        /// 组为字节流
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        public static byte[] ToBytes(this uint num)
        {
            return BitConverter.GetBytes(num);
        }
        /// <summary>
        /// 转为字节数组
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        public static byte[] ToBytes(this int num)
        {
            return BitConverter.GetBytes(num);
        }
        /// <summary>
        /// 转为字节数组
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        public static byte[] ToBytes(this long num)
        {
            return BitConverter.GetBytes(num);
        }
        /// <summary>
        /// 转为字节数组
        /// </summary>
        /// <param name="num"></param>
        /// <returns></returns>
        public static byte[] ToBytes(this ulong num)
        {
            return BitConverter.GetBytes(num);
        }
        /// <summary>
        /// 截取UInt16数据
        /// </summary>
        /// <param name="data"></param>
        /// <param name="startIndex"></param>
        /// <returns></returns>
        public static ushort ToUInt16(this byte[] data, int startIndex)
        {
            return BitConverter.ToUInt16(data, startIndex);
        }
        /// <summary>
        /// 截取UInt16数据
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static ushort ToUInt16(this byte[] data)
        {
            return BitConverter.ToUInt16(data, 0);
        }
        /// <summary>
        /// 截取UInt32数据
        /// </summary>
        /// <param name="data"></param>
        /// <param name="startIndex"></param>
        /// <returns></returns>
        public static uint ToUInt32(this byte[] data, int startIndex)
        {
            return BitConverter.ToUInt32(data, startIndex);
        }
        /// <summary>
        /// 截取UInt32数据
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static uint ToUInt32(this byte[] data)
        {
            return BitConverter.ToUInt32(data, 0);
        }
        /// <summary>
        /// 截取Int32数据
        /// </summary>
        /// <param name="data"></param>
        /// <param name="startIndex"></param>
        /// <returns></returns>
        public static int ToInt32(this byte[] data, int startIndex)
        {
            return BitConverter.ToInt32(data, startIndex);
        }
        /// <summary>
        /// 截取Int32数据
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static int ToInt32(this byte[] data)
        {
            return BitConverter.ToInt32(data, 0);
        }
        /// <summary>
        /// 截取Int64数据
        /// </summary>
        /// <param name="data"></param>
        /// <param name="startIndex"></param>
        /// <returns></returns>
        public static long ToInt64(this byte[] data, int startIndex)
        {
            return BitConverter.ToInt64(data, startIndex);
        }
        /// <summary>
        /// 截取Int64数据
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static long ToInt64(this byte[] data)
        {
            return BitConverter.ToInt64(data, 0);
        }
        /// <summary>
        /// 截取Int64数据
        /// </summary>
        /// <param name="data"></param>
        /// <param name="startIndex"></param>
        /// <returns></returns>
        public static ulong ToUInt64(this byte[] data, int startIndex)
        {
            return BitConverter.ToUInt64(data, startIndex);
        }
        /// <summary>
        /// 截取Int64数据
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static ulong ToUInt64(this byte[] data)
        {
            return BitConverter.ToUInt64(data, 0);
        }
        /// <summary>
        /// 大端模式取ushort
        /// </summary>
        /// <param name="data"></param>
        /// <param name="startIndex"></param>
        /// <returns></returns>
        public static ushort GetBigEndianUInt16(this byte[] data, int startIndex)
        {
            byte[] arr = new byte[2];
            Array.Copy(data, startIndex, arr, 0, arr.Length);
            return arr.Revert().ToUInt16();
        }
        /// <summary>
        /// 大端模式取uint
        /// </summary>
        /// <param name="data"></param>
        /// <param name="startIndex"></param>
        /// <returns></returns>
        public static uint GetBigEndianUInt32(this byte[] data, int startIndex)
        {
            byte[] arr = new byte[4];
            Array.Copy(data, startIndex, arr, 0, arr.Length);
            return arr.Revert().ToUInt32();
        }
        /// <summary>
        /// 大端模式取ulong
        /// </summary>
        /// <param name="data"></param>
        /// <param name="startIndex"></param>
        /// <returns></returns>
        public static ulong GetBigEndianUInt64(this byte[] data, int startIndex)
        {
            byte[] arr = new byte[8];
            Array.Copy(data, startIndex, arr, 0, arr.Length);
            return arr.Revert().ToUInt64();
        }
        /// <summary>
        /// 转为大端模式字节流
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static byte[] ToBigEndian(this short data)
        {
            return data.ToBytes().Revert();
        }
        /// <summary>
        /// 转为大端模式字节流
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static byte[] ToBigEndian(this ushort data)
        {
            return data.ToBytes().Revert();
        }
        /// <summary>
        /// 转为大端模式字节流
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static byte[] ToBigEndian(this int data)
        {
            return data.ToBytes().Revert();
        }
        /// <summary>
        /// 转为大端模式字节流
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static byte[] ToBigEndian(this uint data)
        {
            return data.ToBytes().Revert();
        }
        /// <summary>
        /// 转为大端模式字节流
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static byte[] ToBigEndian(this long data)
        {
            return data.ToBytes().Revert();
        }
        /// <summary>
        /// 转为大端模式字节流
        /// </summary>
        /// <param name="data"></param>
        /// <returns></returns>
        public static byte[] ToBigEndian(this ulong data)
        {
            return data.ToBytes().Revert();
        }
    }
}
